package com.xunshibao.action.info;

import java.util.Arrays;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunshibao.core.dao.page.Pagination;
import com.xunshibao.entity.info.TutorInfo;
import com.xunshibao.service.info.TutorServiceImpl;
import com.xunshibao.web.IpAddress;
import com.xunshibao.web.SessionUser;
import com.xunshibao.web.security.Security;

@Controller
@RequestMapping(value = "/tutor/*")
@Security
public class TutorInfoController {
	TutorServiceImpl service;

	public void setService(TutorServiceImpl service) {
		this.service = service;
	}

	@RequestMapping("view/{id}")
	public String view(@PathVariable Long id, Model model) {
		TutorInfo info = this.service.getTutorInfo(id);
		if(info != null) {
			model.addAttribute("status", true);
			model.addAttribute("info", info);
		} else {
			model.addAttribute("status", false);
			model.addAttribute("message", "你要查看的信息不存在");
		}
		return "tutor/view";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newInfo() {
		return "tutor/new";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public @ResponseBody
	Model save(TutorInfo info, SessionUser user, IpAddress ip, Model model) {
		if (info != null) {
			int flag = service.postTutorInfo(info, user.getUserId(), ip.getValue());
			model.addAttribute("status", flag);
		} else {
			model.addAttribute("message", "参数错误，请正确填写参数再提交");
		}
		return model;
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.POST)
	public @ResponseBody
	Model edit(@PathVariable("id") Long id, TutorInfo info, SessionUser user, IpAddress ip, Model model) {
		if (info != null) {
			int flag = service.updateTutorInfo(info, user.getUserId(), ip.getValue());
			model.addAttribute("status", flag);
		} else {
			model.addAttribute("message", "参数错误，请正确填写参数再提交");
		}
		return model;
	}

	@RequestMapping(value = { "mylist", "mylist/{type}", "mylist/{type}/{pageNo}/{pageSize}" }, method = RequestMethod.GET)
	public String listmine(SessionUser user, String type, Integer pageNo, Integer pageSize, Model model) {
		TutorInfo.Status status = retriveStatus(type, model);

		if (pageNo == null) {
			pageNo = 1;
			pageSize = 20;
		}
		Pagination page = service.listMyTutorInfo(user.getUserId(), status, pageNo, pageSize);
		model.addAttribute("page", page);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("pageSize", pageSize);

		return "tutor/mylist";
	}

	@RequestMapping(value = { "list", "list/{status}", "mylist/{type}/{pageNo}/{pageSize}" }, method = RequestMethod.GET)
	public String list(@PathVariable("status") String status, @RequestParam("t") Integer subject, Integer pageNo, Integer pageSize, Model model) {
		TutorInfo.Status st = retriveStatus(status, model);
		if (pageNo == null) {
			pageNo = 1;
			pageSize = 20;
		}
		Pagination page = null;
		if (subject == null) {
			page = service.listAll(st, pageNo, pageSize);
		} else {
			page = service.listBySubject(subject, st, pageNo, pageSize);
		}

		model.addAttribute("page", page);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("pageSize", pageSize);
		
		return "tutor/list";
	}
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public @ResponseBody Model delete(SessionUser user, @RequestParam("id") Long[] id, Model model) {
		int result = 0;
		if(user.isAdmin()) {
			result = service.delTutorInfo(Arrays.asList(id));
		} else {
			result = service.delMine(Arrays.asList(id), user.getUserId());
		}
		model.addAttribute("status", result);
		return model;
	}
	

	private TutorInfo.Status retriveStatus(String status, Model model) {
		TutorInfo.Status st = null;
		if (status != null) {
			model.addAttribute("type", status);
			if ("start".equals(status)) {
				st = TutorInfo.Status.START;
			} else if ("end".equals(status)) {
				st = TutorInfo.Status.END;
			}
		}
		return st;
	}

}
